﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model = BLL.Model;

namespace DAL.Translator
{
    public class OrderTranslator
    {
        public static DbEntity.Order TranslateBusinessToDbEntity(Model.Order request,DbEntity.OrderContainer container)
        {
            DbEntity.Order response = null;
            if (request != null)
            {
                if (request.OrderId > 0)
                {
                    response = GetOrderById(request.OrderId,container);
                    response.CreatedDate = request.CreatedDate;

                }
                else
                {
                    response = new DbEntity.Order();
                    response.OrderId = request.OrderId;
                    response.CreatedDate = request.CreatedDate;
                }
                if (request.OrderItems != null && request.OrderItems.Count > 0)
                {
                    foreach (Model.OrderItem item in request.OrderItems)
                    {
                        response.OrderItem.Add(OrderItemTranslator.TranslateBusinessToDbEntity(item,container));
                    }
                }
            }
            return response;
        }

        private static DbEntity.Order GetOrderById(int orderId, DbEntity.OrderContainer container)
        {
            DbEntity.Order orderEntity = null;
            orderEntity = (from o in container.Orders where o.OrderId == orderId select o).FirstOrDefault<DbEntity.Order>();
            return orderEntity;
        }
    }
}
